package com.landiao.service;

import java.io.Serializable;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.landiao.common.pojo.Page;
import com.landiao.common.pojo.Querier;
import com.landiao.mapper.BaseMapper;

/**
 * 功能说明:
 * <p>
 * BaseService.java
 */
public abstract class BaseService<T,PK extends Serializable> {

	protected BaseMapper<T, PK> baseMapper;

	@Autowired
	public void setBaseMapper(BaseMapper<T, PK> baseMapper) {
		this.baseMapper = baseMapper;
	}
	
	protected BaseMapper<T, PK> getBaseMapper(){
		return this.baseMapper;
	}
	 /**
     * 保存单个实体
     *
     * @param m 实体
     * @return 返回保存的实体
     */
    public void save(T record) {
        baseMapper.insert(record);
    }

    /**
     * 更新单个实体
     *
     * @param m 实体
     * @return 返回更新的实体
     */
    public void update(T record) {
    	baseMapper.updateByPrimaryKey(record);
    }

    /**
     * 根据主键删除相应实体
     *
     * @param id 主键
     */
    public void delete(PK id) {
        baseMapper.deleteByPrimaryKey(id);
    }

    /**
     * 按照主键查询
     *
     * @param id 主键
     * @return 返回id对应的实体
     */
    public T getById(PK id) {
        return baseMapper.selectByPrimaryKey(id);
    }
    
    /**
     * 分页查询
     *
     */
    public Page<T> queryByPage(Querier querier,T t) {
    	Page<T> page = new Page<T>(querier);
    	List<T> list = baseMapper.queryByPage(t, page);
    	page.setRows(list);
        return page;
    }
}
